package com.atguigu.dga.governance.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.governance.bean.GovernanceAssessGlobal;
import com.atguigu.dga.governance.bean.GovernanceAssessTecOwner;
import com.atguigu.dga.governance.service.GovernanceAssessDetailService;
import com.atguigu.dga.governance.service.GovernanceAssessGlobalService;
import com.atguigu.dga.governance.service.GovernanceAssessTecOwnerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 治理考评结果明细 前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2024-07-27
 */
@RestController
@RequestMapping("/governance")
public class GovernanceAssessController {

    @Autowired
    GovernanceAssessGlobalService governanceAssessGlobalService;

    @Autowired
    GovernanceAssessTecOwnerService governanceAssessTecOwnerService;
    @Autowired
    GovernanceAssessDetailService governanceAssessDetailService;

    @GetMapping("/globalScore")
    public String getGlobalScore(){
        QueryWrapper<GovernanceAssessGlobal> queryWrapper = new QueryWrapper<GovernanceAssessGlobal>()
                .inSql("assess_date", "select max(assess_date) from governance_assess_global");
        GovernanceAssessGlobal governanceAssessGlobal = governanceAssessGlobalService.getOne(queryWrapper);

        JSONObject resultJsonObj = new JSONObject();
        resultJsonObj.put("assessDate",governanceAssessGlobal.getAssessDate());
        resultJsonObj.put("sumScore",governanceAssessGlobal.getScore());

        List<BigDecimal> scoreList =  new ArrayList<>();
        scoreList.add( governanceAssessGlobal.getScoreSpec());
        scoreList.add( governanceAssessGlobal.getScoreStorage());
        scoreList.add( governanceAssessGlobal.getScoreCalc());
        scoreList.add( governanceAssessGlobal.getScoreQuality());
        scoreList.add( governanceAssessGlobal.getScoreSecurity());

        resultJsonObj.put("scoreList",scoreList);
        return resultJsonObj.toJSONString();
    }

    @GetMapping("/rankList")
    public String getRankList(){
        QueryWrapper<GovernanceAssessTecOwner> queryWrapper = new QueryWrapper<GovernanceAssessTecOwner>()
                .select("tec_owner as tecOwner","score")
                .orderByDesc("score")
                .inSql("assess_date", "select max(assess_date) from governance_assess_tec_owner");

        List<Map<String, Object>> mapList = governanceAssessTecOwnerService.listMaps(queryWrapper);

        return  JSON.toJSONString(mapList) ;
   }

   @GetMapping("/problemNum")
    public String getProblemNum(){
       QueryWrapper<GovernanceAssessDetail> queryWrapper = new QueryWrapper<GovernanceAssessDetail>()
               .select("sum(if(governance_type='SPEC',1,0)) as SPEC" ,
                       "sum(if(governance_type='STORAGE',1,0)) as STORAGE" ,
                       "sum(if(governance_type='CALC',1,0)) as CALC" ,
                       "sum(if(governance_type='QUALITY',1,0)) as QUALITY" ,
                       "sum(if(governance_type='SECURITY',1,0)) as SECURITY"
                       )
               .lt("assess_score",BigDecimal.TEN)
               .inSql("assess_date", "select max(assess_date) from governance_assess_detail");
       Map<String, Object> map = governanceAssessDetailService.getMap(queryWrapper);
       return JSON.toJSONString(map);


   }

   @GetMapping("/problemList/{type}/{pageNo}/{pageSize}")
   public String getProblemList(@PathVariable("type") String type,
                                @PathVariable("pageNo") Integer pageNo,
                                @PathVariable("pageSize")Integer pageSize){

        QueryWrapper<GovernanceAssessDetail> queryWrapper = new QueryWrapper<GovernanceAssessDetail>()
                .eq("governance_type",type)
                .lt("assess_score",BigDecimal.TEN)
                .inSql("assess_date", "select max(assess_date) from governance_assess_detail")
                .last("limit "+(pageNo-1)*pageSize+","+pageSize);

        List<GovernanceAssessDetail> list = governanceAssessDetailService.list(queryWrapper);
        return JSON.toJSONString(list);

   }

}
